package edu.gzy.rpsm_property.mapper;

import com.github.pagehelper.Page;
import edu.gzy.rpsm_property.pojo.Amount;
import edu.gzy.rpsm_property.pojo.Pay;
import edu.gzy.rpsm_property.pojo.vo.PayInfo;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.util.List;

public interface PayMapper {
    @Select("<script>" +
            "SELECT payInfo.pay_id, payInfo.building_id, payInfo.room_id, u.userid " +
            "FROM (SELECT p.pay_id, b.building_id, r.room_id, f.floor_id FROM " +
            "(SELECT pay.pay_id, pay.pay_room_id FROM pay " +
            "<where><if test='start != null and end != null'> pay.pay_date BETWEEN #{start} AND #{end} </if></where>) p, " +
            "building b, " +
            "(SELECT r.room_id, r.room_floor_id FROM room r " +
            "<where><if test='floorId != null'> r.room_floor_id = #{floorId} </if></where> " +
            "UNION SELECT room.room_id, room.room_floor_id FROM room, floor WHERE " +
            "<if test='buildingId != null and floorId == null'> floor.belong_building = #{buildingId} AND </if> " +
            "room.room_floor_id = floor.floor_id) r, floor f " +
            "WHERE r.room_floor_id = f.floor_id AND f.belong_building = b.building_id AND p.pay_room_id = r.room_id) payInfo " +
            "LEFT JOIN (SELECT userid, user_roomid FROM `user` " +
            "<where><if test='username != null and username != \"\"'> username LIKE CONCAT('%', #{username}, '%') </if> " +
            "<if test='idCard != null and idCard != \"\"'> AND idcard LIKE CONCAT('%', #{idCard}, '%') </if></where>) u " +
            "ON u.user_roomid = payInfo.room_id " +
            "</script>")
    @Results(id = "payInfo", value = {
            @Result(property = "user", column = "userid", one = @One(select = "edu.gzy.rpsm_property.mapper.UserMapper.queryById")),
            @Result(property = "building", column = "building_id", one = @One(select = "edu.gzy.rpsm_property.mapper.BuildingMapper.queryById")),
            @Result(property = "room", column = "room_id", one = @One(select = "edu.gzy.rpsm_property.mapper.RoomMapper.roomById")),
            @Result(property = "pay", column = "pay_id", one = @One(select = "edu.gzy.rpsm_property.mapper.PayMapper.queryById"))
    })
    Page<PayInfo> selectPayInfoOnCondition(
            @Param("start") LocalDate start,
            @Param("end") LocalDate end,
            @Param("username") String username,
            @Param("idCard") String idCard,
            @Param("floorId") Integer floorId,
            @Param("buildingId") Integer buildingId
    );


    @Select(
            "<script>" +
                    "select * from pay where pay_id = #{id}" +
                    "</script>")
    Pay queryById(Integer id);


    @Select("select * from amount where amount_roomid = #{amount_roomid}")
    Pay unPaidById(Integer id);

    @Insert("INSERT INTO amount (amount_roomid, amount_type, amount_date, amount, isPaied) " +
            "VALUES (#{amount_roomid}, #{amount_type}, #{amount_date}, #{amount}, #{isPaid})")
    int insertAmount(Amount amount);

    @Insert("INSERT INTO pay (pay_room_id,pay, pay_date, pay_type) VALUES (#{pay_room_id},#{pay}, #{pay_date}, #{pay_type})")
    void insertPayment(Pay pay);

    @Select("SELECT * FROM amount WHERE amount_roomid = #{roomId} AND amount_type = #{payType} AND isPaied = 2 " +
            "ORDER BY amount_date ASC ")
    List<Amount>findUnpaidAmountsByRoomAndType(
            @Param("roomId") Integer roomId,
            @Param("payType")Integer payType
    );

    @Update("UPDATE amount SET amount = #{amount.amount}, isPaied = #{amount.isPaid} WHERE amount_id = #{amount.amount_id}")
    int updateAmount(@Param("amount") Amount amount);
    @Select("SELECT SUM(amount) FROM amount WHERE amount_roomid = #{roomId} AND amount_type = #{payType} AND isPaied = 2")
    Integer getTotalUnpaidAmountByRoomAndType(Integer roomId, Integer payType);
}
